package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/3/27 11:06
 * @description
 */
public class Code859 {
    public static void main(String[] args) {
        System.out.println(new Code859().buddyStrings("abcaa", "abcbb"));
    }

    public boolean buddyStrings(String A, String B) {
        int l1 = A.length();
        int l2 = B.length();
        if (l1 < 2 || l2 < 2 || l1 != l2) {
            return false;
        }
        //统计不同
        List<Integer> list = new ArrayList<>(l1);
        for (int i = 0; i < l1; i++) {
            int r = A.charAt(i) - B.charAt(i);
            if (r != 0) {
                list.add(r);
            }
        }
        if (list.size() == 2) {
            return (list.get(0) + list.get(1)) == 0;
        }
        if (list.size() == 0) {
            //看有没有出现两次一样的字母的
            for (int i = 0; i < l1; i++) {
                char c = A.charAt(i);
                if (A.indexOf(c) != A.lastIndexOf(c)) {
                    return true;
                }
            }
        }

        return false;
    }
}
